package com.yi.mall.seckill.service.impl;

import com.yi.mall.seckill.service.IBuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class BuyServiceImpl implements IBuyService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public void dcount(int activityId, String skuId) {
        // 提取商品ID
        String key = "seckill-" + activityId + "-" + skuId;
        // 提取商品库存
        if(redisTemplate.opsForValue().get(key)==null){
            throw new RuntimeException("key不存在");
        }
        int count = Integer.parseInt(redisTemplate.opsForValue().get(key));

        // 如果库存大于0，则减1
        if(count > 0){
            redisTemplate.opsForValue().decrement(key);
        }else {
            throw new RuntimeException("库存不足");
        }
    }
}
